********************************************************************************
********************************************************************************
********************* Regressions on the wedge

clear all
do "$maindir/Code/bootstrapres.do"
use "$maindir/Results/Model_Outputs/ihwap_wedge.dta"


*** prism-related variables
gen rsq_pre = max if treated==0
bysort Household : egen rsquare_pre = max(rsq_pre)
gen rsq_post = max if treated==1
bysort Household : egen rsquare_post = max(rsq_post)

gen hd_pre = HDDbest if treated==0
bysort Household : egen hdd_pre = max(hd_pre)
gen hd_post = HDDbest if treated==1
bysort Household : egen hdd_post = max(hd_post)

* prism sample restriction
gen prismrest = 0 if rsquare_pre!=. & rsquare_post!=. & hdd_pre!=. & hdd_post!=.
replace prismrest = 1 if hdd_pre>=43 & hdd_pre<73 & hdd_post>=43 & hdd_post<73 & ///
		rsquare_pre>=0.85 & rsquare_pre!=. & rsquare_post>=0.85 & rsquare_post!=.

** merge with info about furnace replacements
merge m:1 Household using "$maindir/Data/furn_replace.dta"
drop if _merge==2
drop _merge


***** some extra restrictions on number of homes served by each contractor
gen aux=1
sort Household meterend
bysort Household : gen homeobs = sum(aux) if twoyears_prepost==1 & meterdays_monthly!=. & ProgramYear>=2009
replace homeobs = . if homeobs!=1

sort Household meterend
bysort Household : gen homeobs_post = sum(aux) if treated==1 & twoyears_prepost==1 & meterdays_monthly!=. & ProgramYear>=2009
replace homeobs_post = . if homeobs_post!=1

** fixing builddate to avoid omitted variables
replace builddate = 11 if tab_builddate11==1 | tab_builddate12==1 | tab_builddate13==1

** fixing sqfeet to avoid omitted variables
drop roundsqfeet
gen roundsqfeet = .
	forval i = 0(500)2500 {
		replace roundsqfeet = `i' if sqfeet>`i' & sqfeet<=`i'+500
	}
replace roundsqfeet = 3000 if sqfeet>3000 & sqfeet!=.

** fixing some spending variables to avoid omitted bins
drop binAirCon
gen binAirCon = .
replace binAirCon=0 if Real_tot_actAirCon==0
replace binAirCon=1 if Real_tot_actAirCon>0 & Real_tot_actAirCon!=.

replace binAttic = 2700 if Real_tot_actAttic>2400 & Real_tot_actAttic!=.

** demographic variables
*tabulate group_demographics, gen(tab_demog)
*drop tab_demog14
gen simpocc = noccupants
replace simpocc = 5 if noccupants>=5 & noccupants!=.

gen binAge = .
forval i = 20(10)80 {
replace binAge = `i' if Age>`i' & Age<=`i'+10
}

gen binIncome = .
forval i = 5000(5000)35000 {
replace binIncome = `i' if Real_income>`i' & Real_income<=`i'+5000
}
replace binIncome = 1 if Real_income<=5000
replace binIncome = 40000 if Real_income>40000 & Real_income!=.


*** housing structure
gen binSqft = .
forval i = 600(300)2700 {
replace binSqft = `i' if sqfeet>`i' & sqfeet<=`i'+300
}
replace binSqft = 1 if sqfeet<=600
replace binSqft = 3000 if sqfeet>3000 & sqfeet!=.

drop BlowerPreBins
gen BlowerPreBins = .
forval i = 2000(1000)9000 {
replace BlowerPreBins = `i' if Blower_Pre>`i' & Blower_Pre<=`i'+1000
}
replace BlowerPreBins = 1 if Blower_Pre<=2000
replace BlowerPreBins = 10000 if Blower_Pre>10000 & Blower_Pre!=.

*** energy prices
gen binelec = .
local counter = 2
forval i = 10.5(0.5)12.5 {
replace binelec = `counter' if elec_prices>`i' & elec_prices<=`i'+0.5
local counter = `counter' + 1
}
replace binelec = 1 if elec_prices<10.5
replace binelec = `counter' if elec_prices>13 & elec_prices!=.

gen bingas = .
local counter = 2
forval i = 7(1)16 {
replace bingas = `counter' if gas_prices>`i' & gas_prices<=`i'+0.5
local counter = `counter' + 1
}
replace bingas = 1 if gas_prices<7
replace bingas = `counter' if gas_prices>17 & gas_prices!=.

* transforming decimals in percent
gen savings_gap_pct = savings_gap*100

forval i = 1/200 {
gen savings_gap_pct`i' = 100*savings_gap`i'
}

** counting number of homes served by contractor
merge m:1 Household using "$maindir/Data/ihwap_state.dta", keepusing(arch_contID) nogen keep(3)
sort arch_contID
by arch_contID : egen totjobs = total(aux) if ww_treat==1
by arch_contID : egen totaljobs = max(totjobs)
drop totjobs
replace totaljobs=. if arch_contID==.

*br if totaljobs>700 & totaljobs!=.
* Contractor with ID 236 to be ommitted, being one of the biggest contractors in the sample

** main heat pre-treatment
gen MainHeatkBTU = MainHeatBTU/1000

gen binheatkbtu = .
forval i = 60(10)120 {
replace binheatkbtu = `i' if MainHeatkBTU>`i' & MainHeatkBTU<=`i'+10
}
replace binheatkbtu = 1 if MainHeatkBTU<=60
replace binheatkbtu = 130 if MainHeatkBTU>130 & MainHeatkBTU!=.

gen heatworks = 0 if MainHeatOperational=="False"
replace heatworks = 1 if MainHeatOperational=="True"


* fixing variables that have a zero indicator (for later interacting)
foreach x in nstories binAirCon white black hispanic otherrace haselderly hasminor heatworks {
replace `x' = `x'+1
}
replace roundAtticR = 1 if roundAtticR==0

gen heatnotwork = 1 if MainHeatOperational=="False"
replace heatnotwork = 0 if MainHeatOperational=="True"

replace binFurnace = 1 if binFurnace==0

gen failprism = 0 if prismrest==1
replace failprism = 1 if prismrest==0

* variables for projected savings
gen ExistingConsumption_mmbtu = ExistingConsumption/1000000
gen ProjectedConsumption_mmbtu = ProjectedConsumption/1000000


**** alternative (coarse) binned spending for interactions
gen coarsebinAirSeal = 1 if binAirSeal==1
replace coarsebinAirSeal = 2 if binAirSeal>1 & binAirSeal<=300
replace coarsebinAirSeal = 3 if binAirSeal>300 & binAirSeal<=600
replace coarsebinAirSeal = 4 if binAirSeal>600 & binAirSeal!=.

gen coarsebinWallIns = 1 if binWallIns==1
replace coarsebinWallIns = 2 if binWallIns>1 & binWallIns<=600
replace coarsebinWallIns = 3 if binWallIns>600 & binWallIns<=1200
replace coarsebinWallIns = 4 if binWallIns>1200 & binWallIns<=1800
replace coarsebinWallIns = 5 if binWallIns>1800 & binWallIns<=2400
replace coarsebinWallIns = 6 if binWallIns>2400 & binWallIns!=.

gen coarsebinAttic = 1 if binAttic==1
replace coarsebinAttic = 2 if binAttic>1 & binAttic<=600
replace coarsebinAttic = 3 if binAttic>600 & binAttic<=1200
replace coarsebinAttic = 4 if binAttic>1200 & binAttic<=1800
replace coarsebinAttic = 5 if binAttic>1800 & binAttic<=2400
replace coarsebinAttic = 6 if binAttic>2400 & binAttic!=.

gen coarsebinFoundation = 1 if binFoundation==1
replace coarsebinFoundation = 2 if binFoundation>1 & binFoundation<=400
replace coarsebinFoundation = 3 if binFoundation>400 & binFoundation<=800
replace coarsebinFoundation = 4 if binFoundation>800 & binFoundation<=1200
replace coarsebinFoundation = 5 if binFoundation>1200 & binFoundation<=1600
replace coarsebinFoundation = 6 if binFoundation>1600 & binFoundation!=.

gen coarsebinWindow = 1 if binWindow==1
replace coarsebinWindow = 2 if binWindow>1 & binWindow<=400
replace coarsebinWindow = 3 if binWindow>400 & binWindow<=800
replace coarsebinWindow = 4 if binWindow>800 & binWindow<=1200
replace coarsebinWindow = 5 if binWindow>1200 & binWindow<=1600
replace coarsebinWindow = 6 if binWindow>1600 & binWindow!=.



********************************************************************************
****** finally, regression for the gap
				
**** specification omitting ZERO spending
reg savings_gap_pct ib1.binFurnace ib1.binAirSeal ib1.binAttic ib1.binWallIns ///
		ib1.binBaseload ib1.binDoor ib1.binWtHtr ib1.binHealSfty ///
		ib1.binAirCon ib1.binFoundation ib1.binGeneral ib1.binWindow ///
		ib3000.BlowerPreBins ib70.binheatkbtu ib2.heatworks ///
		ib2.simpocc ib50.binAge ib15000.binIncome ///
		ib1.white ib1.black ib1.hispanic ib1.otherrace ib1.haselderly ib1.hasminor ///
		ib1500.binSqft ib1.nstories ib1.roundAtticR ib1.builddate ///
		ib2011.ProgramYear ib236.arch_contID ///
		c.tmin c.tmin#c.tmin c.tmax c.tmax#c.tmax c.precip c.precip#c.precip ///
		i.end_month elec_prices gas_prices ///
		if twoyears_prepost==1 & meterdays_monthly!=. & ProgramYear>=2009 & treated==1 & totaljobs>=20 & totaljobs!=. ///
		, vce(cluster Household)

est sto gapreg
gen gapsamp = e(sample)
matrix gapbetas = e(b)
matrix gapvars = e(V)
sca nobs = e(N)
sca rsq = e(r2)
sca rss_full = e(rss)

** count number of contractors in the regression		
qui: distinct arch_contID if gapsamp==1
sca nconts = r(ndistinct)
di "Number of contractors = `=scalar(nconts)'"

		
		
********************************************************************************
*************** simulate how gap is affected by zero spending on some measures

est restore gapreg
predict pred_pctgap
order pred_pctgap, before(savings_gap_pct)

** zero spending on Wall Insulation
rename binWallIns origbinWallIns
gen binWallIns = origbinWallIns
replace binWallIns = 1 if binWallIns!=. & binWallIns!=1

predict pred_WallIns0

drop binWallIns
rename origbinWallIns binWallIns

** zero spending on Furnace
rename binFurnace origbinFurnace
gen binFurnace = origbinFurnace
replace binFurnace = 1 if binFurnace!=. & binFurnace!=1

predict pred_Furnace0

drop binFurnace
rename origbinFurnace binFurnace

** no Furnace replacements (no spending above 1800)
rename binFurnace origbinFurnace
gen binFurnace = origbinFurnace
replace binFurnace = 1 if binFurnace!=. & binFurnace>=2100

predict pred_Furnace2100

drop binFurnace
rename origbinFurnace binFurnace

** no Attic spending above 2400
rename binAttic origbinAttic
gen binAttic = origbinAttic
replace binAttic = 1 if binAttic!=. & binAttic>=2700

predict pred_Attic2700

drop binAttic
rename origbinAttic binAttic

** no Window spending above 1400
rename binWindow origbinWindow
gen binWindow = origbinWindow
replace binWindow = 1 if binWindow!=. & binWindow>=1600

predict pred_Window1600

drop binWindow
rename origbinWindow binWindow



** all of the measures combined
* zero spending on Wall Insulation, no Window spending above 1400,  no Attic spending above 2400, zero furnace spending

rename binWallIns origbinWallIns
gen binWallIns = origbinWallIns
replace binWallIns = 1 if binWallIns!=. & binWallIns!=1

rename binWindow origbinWindow
gen binWindow = origbinWindow
replace binWindow = 1 if binWindow!=. & binWindow>=1600

rename binFurnace origbinFurnace
gen binFurnace = origbinFurnace
replace binFurnace = 1 if binFurnace!=. & binFurnace!=1

rename binAttic origbinAttic
gen binAttic = origbinAttic
replace binAttic = 1 if binAttic!=. & binAttic>=2700

predict pred_allzero

drop binWindow
rename origbinWindow binWindow

drop binWallIns
rename origbinWallIns binWallIns

drop binFurnace
rename origbinFurnace binFurnace

drop binAttic
rename origbinAttic binAttic


********** average predicted (simulated) gap
foreach x in savings_gap_pct pred_pctgap pred_Furnace2100 pred_WallIns0 pred_Furnace0 pred_Attic2700 pred_Window1600 pred_allzero {
	reg `x' i.aux if gapsamp==1  , nocons cluster(Household)
	est sto est`x'
	matrix mean_`x' = e(b)
	sca scamean_`x' = mean_`x'[1,1]
}

foreach x in pred_Furnace2100 pred_WallIns0 pred_Furnace0 pred_Attic2700 pred_Window1600 pred_allzero {
	sca pctdiff_`x' = 100*(scamean_`x' - scamean_savings_gap_pct)/scamean_savings_gap_pct 
	mat matpctdiff_`x' = (pctdiff_`x')
}
		
drop pred_pctgap pred_Furnace2100 pred_WallIns0 pred_Furnace0 pred_Attic2700 pred_Window1600 pred_allzero 		
		
********************************************************************************		
** bootstrapping standard errors for gap regression

local dim `= rowsof(gapbetas)',`=colsof(gapbetas)'
matrix gap_coefs_boot2 = J(`dim',.)
matrix sim_savings_gap_pct = (., .)
matrix sim_pred_pctgap = (., .)
matrix sim_pred_Furnace2100 = (., .)
matrix sim_pred_WallIns0 = (., .)
matrix sim_pred_Furnace0 = (., .)
matrix sim_pred_Attic2700 = (., .)
matrix sim_pred_Window1600 = (., .)
matrix sim_pred_allzero = (., .)

rename savings_gap_pct orig_savings_gap_pct

forval i = 1/200 {
	di "starting bootstrap iteration `i'"
	qui : reg savings_gap_pct`i' ib1.binFurnace ib1.binAirSeal ib1.binAttic ib1.binWallIns ///
			ib1.binBaseload ib1.binDoor ib1.binWtHtr ib1.binHealSfty ///
			ib1.binAirCon ib1.binFoundation ib1.binGeneral ib1.binWindow ///
			ib3000.BlowerPreBins ib70.binheatkbtu ib2.heatworks ///
			ib2.simpocc ib50.binAge ib15000.binIncome ///
			ib1.white ib1.black ib1.hispanic ib1.otherrace ib1.haselderly ib1.hasminor ///
			ib1500.binSqft ib1.nstories ib1.roundAtticR ib1.builddate ///
			ib2011.ProgramYear ib236.arch_contID ///
			c.tmin c.tmin#c.tmin c.tmax c.tmax#c.tmax c.precip c.precip#c.precip ///
			i.end_month elec_prices gas_prices ///
			[fweight = weight_boots`i'] ///
			if twoyears_prepost==1 & meterdays_monthly!=. & ProgramYear>=2009 & treated==1 & totaljobs>=20 & totaljobs!=.

	est sto bootsgapreg
	matrix tempmat = e(b)
	matrix gap_coefs_boot2 = (gap_coefs_boot2 \ tempmat)
	gen bootsgapsamp = e(sample)



	********************************************************************************
	************ bootstrapping - simulate how gap is affected by zero spending on some measures

	est restore bootsgapreg
	predict pred_pctgap

	** zero spending on Wall Insulation
	rename binWallIns origbinWallIns
	gen binWallIns = origbinWallIns
	replace binWallIns = 1 if binWallIns!=. & binWallIns!=1

	predict pred_WallIns0

	drop binWallIns
	rename origbinWallIns binWallIns

	** zero spending on Furnace
	rename binFurnace origbinFurnace
	gen binFurnace = origbinFurnace
	replace binFurnace = 1 if binFurnace!=. & binFurnace!=1

	predict pred_Furnace0

	drop binFurnace
	rename origbinFurnace binFurnace

	** no Furnace replacements (no spending above 1800)
	rename binFurnace origbinFurnace
	gen binFurnace = origbinFurnace
	replace binFurnace = 1 if binFurnace!=. & binFurnace>=2100

	predict pred_Furnace2100

	drop binFurnace
	rename origbinFurnace binFurnace

	** no Attic spending above 2400
	rename binAttic origbinAttic
	gen binAttic = origbinAttic
	replace binAttic = 1 if binAttic!=. & binAttic>=2700

	predict pred_Attic2700

	drop binAttic
	rename origbinAttic binAttic

	** no Window spending above 1400
	rename binWindow origbinWindow
	gen binWindow = origbinWindow
	replace binWindow = 1 if binWindow!=. & binWindow>=1600

	predict pred_Window1600

	drop binWindow
	rename origbinWindow binWindow


	** all of the measures combined
	* zero spending on Wall Insulation, no Window spending above 1400,  no Attic spending above 2400, zero furnace spending

	rename binWallIns origbinWallIns
	gen binWallIns = origbinWallIns
	replace binWallIns = 1 if binWallIns!=. & binWallIns!=1

	rename binWindow origbinWindow
	gen binWindow = origbinWindow
	replace binWindow = 1 if binWindow!=. & binWindow>=1600

	rename binFurnace origbinFurnace
	gen binFurnace = origbinFurnace
	replace binFurnace = 1 if binFurnace!=. & binFurnace!=1

	rename binAttic origbinAttic
	gen binAttic = origbinAttic
	replace binAttic = 1 if binAttic!=. & binAttic>=2700

	predict pred_allzero

	drop binWindow
	rename origbinWindow binWindow

	drop binWallIns
	rename origbinWallIns binWallIns

	drop binFurnace
	rename origbinFurnace binFurnace

	drop binAttic
	rename origbinAttic binAttic

	rename savings_gap_pct`i' savings_gap_pct
	
	********** average predicted (simulated) gap
	qui : reg savings_gap_pct i.aux [fweight = weight_boots`i'] if bootsgapsamp==1  , nocons
	matrix tempcoef = e(b)
	sca meanfordiff = tempcoef[1,1]
	
	foreach x in savings_gap_pct pred_pctgap pred_Furnace2100 pred_WallIns0 pred_Furnace0 pred_Attic2700 pred_Window1600 pred_allzero {
		qui : reg `x' i.aux [fweight = weight_boots`i'] if bootsgapsamp==1  , nocons
		matrix tempcoef = e(b)
		sca scamean_`x' = tempcoef[1,1]
				
		sca pctgapdiff = (scamean_`x' - meanfordiff)/meanfordiff 
		sca temppct = 100*(pctgapdiff)
		
		matrix sim_`x' = (sim_`x' \ (scamean_`x', temppct))
	}


	drop pred_pctgap pred_Furnace2100 pred_WallIns0 pred_Furnace0 pred_Attic2700 pred_Window1600 pred_allzero bootsgapsamp		
	rename savings_gap_pct savings_gap_pct`i'
}
rename orig_savings_gap_pct savings_gap_pct


****** output coefficients with bootstrapped stanard errors
matrix gapcoefs = gapbetas
matrix bootstrapcoefs2 = gap_coefs_boot2
mata : st_matrix("bootstrapcoefs2", editvalue(st_matrix("bootstrapcoefs2"), 0, .))
mata : st_matrix("gap_var_boot2", mm_colvar(st_matrix("bootstrapcoefs2")))
matrix gap_var_boot2 = diag(gap_var_boot2)
mata : st_matrix("gap_var_boot2", editvalue(st_matrix("gap_var_boot2"), ., 0))
local colnames : colnames gapbetas
mat colnames gap_var_boot2=`colnames'
mat rownames gap_var_boot2=`colnames'
bootstrapres gapcoefs gap_var_boot2 nobs
est sto decompose_gap_inter
est save "$maindir/Results/Model_Outputs/decompose_wedge", replace
parmest, saving("$maindir/Results/Model_Outputs/decompose_wedge.dta", replace)		


****** output simulations with bootstrapped standard errors
matrix main_gap = sim_savings_gap_pct[2..., 1]
mata : st_matrix("main_gap_var", mm_colvar(st_matrix("main_gap")))
matrix main_gap = mean_savings_gap_pct
matrix main_gap_pct = sim_savings_gap_pct[2..., 2]
mata : st_matrix("main_gap_pct_var", mm_colvar(st_matrix("main_gap_pct")))
matrix main_gap_pct = (0)

foreach x in pred_Furnace2100 pred_WallIns0 pred_Furnace0 pred_Attic2700 pred_Window1600 pred_allzero {
	matrix sim_gap = sim_`x'[2..., 1]
	mata : st_matrix("sim_gap_var", mm_colvar(st_matrix("sim_gap")))
	matrix sim_gap_pct = sim_`x'[2..., 2]
	mata : st_matrix("sim_gap_pct_var", mm_colvar(st_matrix("sim_gap_pct")))

	matrix main_gap = (main_gap, mean_`x')
	matrix main_gap_var = (main_gap_var, sim_gap_var)
	matrix main_gap_pct = (main_gap_pct, matpctdiff_`x')
	matrix main_gap_pct_var = (main_gap_pct_var, sim_gap_pct_var)
}


local counter = 1
foreach x in savings_gap_pct pred_Furnace2100 pred_WallIns0 pred_Furnace0 pred_Attic2700 pred_Window1600 pred_allzero {

	matrix curmod = main_gap[1, `counter']
	matrix curmod_var = main_gap_var[1, `counter']
	mat rownames curmod_var = "c1"

	matrix curmodpct = main_gap_pct[1, `counter']
	matrix curmodpct_var = main_gap_pct_var[1, `counter']
	mat rownames curmodpct_var = "c1"

	bootstrapres curmod curmod_var nobs
	est sto est_`x'

	bootstrapres curmodpct curmodpct_var nobs
	est sto estpct_`x'

	local counter = `counter' + 1
}


esttab est_savings_gap_pct est_pred_Furnace2100 est_pred_WallIns0 est_pred_Furnace0 est_pred_Attic2700 est_pred_Window1600 est_pred_allzero ///
				using "$maindir/Results/Tables/simul_gap_results.tex", ///
				replace b(3) se(3) nostar nonumbers ///
				nonotes stats(N, fmt(%9.0gc) labels("Observations")) ///
				mlabels("Baseline" "No Furnace Replace" "Zero WallIns" "Zero Furnace" "No Large Attic" "No Large Window" "All Combined")
				
				
esttab estpct_savings_gap_pct estpct_pred_Furnace2100 estpct_pred_WallIns0 estpct_pred_Furnace0 estpct_pred_Attic2700 estpct_pred_Window1600 estpct_pred_allzero ///
				using "$maindir/Results/Tables/simul_gap_pctresults.tex", ///
				replace b(3) se(3) nostar nonumbers ///
				nonotes stats(N, fmt(%9.0gc) labels("Observations")) ///
				mlabels("Baseline" "No Furnace Replace" "Zero WallIns" "Zero Furnace" "No Large Attic" "No Large Window" "All Combined")		

				

				
********************************************************************************
********* HISTOGRAMS FOR DOLLARS SPENT IN EACH BIN
gen newbinAirSeal = binAirSeal - 100
replace newbinAirSeal = -100 if newbinAirSeal==-99
gen newbinAttic = binAttic - 300
replace newbinAttic = -300 if newbinAttic==-299
gen newbinWallIns = binWallIns - 300
replace newbinWallIns = -300 if newbinWallIns==-299
gen newbinFurnace = binFurnace - 300
replace newbinFurnace = -300 if newbinFurnace==-299
gen newbinWindow = binWindow - 200
replace newbinWindow = -200 if newbinWindow==-199

replace gapsamp=. if gapsamp!=1
sort Household gapsamp
bysort Household : gen gaphomes = sum(aux) if gapsamp==1
replace gaphomes = . if gaphomes!=1

foreach x of varlist binFurnace binAirSeal binAttic binWallIns binWindow {
bysort new`x' gaphomes : gen count`x' = _n if gaphomes==1
bysort new`x' : egen freq_`x' = max(count`x') if gaphomes==1
drop count`x'
}		

label var Real_tot_actAirCon "Air Conditioning"
label var Real_tot_actAirSeal "Air Sealing"
label var Real_tot_actAttic "Attic"
label var Real_tot_actBaseload "Baseload"
label var Real_tot_actDoor "Door"
label var Real_tot_actFoundation "Foundation"
label var Real_tot_actFurnace "Furnace"
label var Real_tot_actGeneral "General"
label var Real_tot_actHealSfty "Health and Safety"
label var Real_tot_actWallIns "Wall Insulation"
label var Real_tot_actWindow "Window"
label var Real_tot_actWtHtr "Water Heater"

label define furnacelab2 -300 "0" 0 "1-300" 300 "301-600" 600 "601-900" 900 "901-1200" 1200 "1201-1500" ///
		1500 "1501-1800" 1800 "1801-2100" 2100 "2101-2400" 2400 "2401-2700" 2700 "2701-3000" 3000 ">3000"
label values newbinFurnace furnacelab2

label define wallinslab2 -300 "0" 0 "1-300" 300 "301-600" 600 "601-900" 900 "901-1200" 1200 "1201-1500" ///
		1500 "1501-1800" 1800 "1801-2100" 2100 "2101-2400" 2400 "2401-2700" 2700 ">2700"
label values newbinWallIns wallinslab2

label define airseallab2 -100 "0" 0 "1-100" 100 "101-200" 200 "201-300" 300 "301-400" 400 "401-500" ///
		500 "501-600" 600 "601-700" 700 "701-800" 800 "801-900" 900 ">900"		
label values newbinAirSeal airseallab2
		
label define atticlab2 -300 "0" 0 "1-300" 300 "301-600" 600 "601-900" 900 "901-1200" 1200 "1201-1500" ///
		1500 "1501-1800" 1800 "1801-2100" 2100 "2101-2400" 2400 ">2400"		
label values newbinAttic atticlab2

label define windowlab2 -200 "0" 0 "1-200" 200 "201-400" 400 "401-600" 600 "601-800" 800 "801-1000" ///
		1000 "1001-1200" 1200 "1201-1400" 1400 "1401-1600" 1600 "1601-1800" 1800 "1801-2000" 2000 ">2000"
label values newbinWindow windowlab2


count if gaphomes==1
sca totsamp = r(N)
foreach x in Furnace AirSeal Attic Window {
	count if Real_tot_act`x'!=. & gaphomes==1
	sca `x'_samp = r(N)
	gen `x'_percsamp = round((`x'_samp/totsamp)*100, 0.1)
}
foreach x in WallIns {
	count if Real_tot_act`x'>0 & gaphomes==1
	sca `x'_samp = r(N)
	gen `x'_percsamp = round((`x'_samp/totsamp)*100, 0.1)
}

	
keep newbinAirSeal newbinAttic newbinWallIns newbinFurnace newbinWindow ///
	freq_binFurnace freq_binAirSeal freq_binAttic freq_binWallIns freq_binWindow ///
	Furnace_percsamp AirSeal_percsamp Attic_percsamp Window_percsamp WallIns_percsamp
duplicates drop	

foreach x in Attic Furnace WallIns {
gen id_`x' = newbin`x'/300 + 2
order id_`x', after(newbin`x')
}
foreach x in Window {
gen id_`x' = newbin`x'/200 + 2
order id_`x', after(newbin`x')
}
foreach x in AirSeal {
gen id_`x' = newbin`x'/100 + 2
order id_`x', after(newbin`x')
}

foreach x in AirSeal Attic Furnace WallIns Window {
	drop if freq_bin`x'==.
}
duplicates drop


***** exporting data to be used for overlay histograms
save "$maindir/Results/Model_Outputs/histogram_data.dta", replace
